<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:登录管理
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\BackendController;
use support\Request;
use think\facade\Db;
use Webman\Event\Event;
class LoginController extends BackendController
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {

        return view("login/index");
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function checkLogin(Request $request)
    {
        $r = Db::name("user")->alias("a")->join("admin b", "a.u_id=b.u_id")->whereRaw("a.username=:username and  a.password=:password", ["username" => $request->post("username"), "password" => encrypt($request->post("password"))])->find();
        $res;
        if ($r) {
            $res['status'] = 1;
            $res['msg']    = '登陆成功！';
            $session       = $request->session();
            $session->set("admin_id", $r['u_id']);
            $session->set("admin_info", $r);
            //触发事件
            Event::emit('syslog.handle', $res);

        } else {
            $res['status'] = 0;
            $res['msg']    = '登陆失败，请核对账号密码！';
        }
        return json($res);
    }

    public function logout(Request $request)
    {
        $session = $request->session();
        $session->delete("admin_id");
        $session->delete("admin_info");
        return redirect('/admin/index/index');

    }

}
